package com.yh.autobio.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.yh.autobio.AppConfig;
import com.yh.autobio.AppContext;
import com.yh.autobio.R;
import com.yh.autobio.base.BaseFragment;
import com.yh.autobio.common.UIHelper;
import com.yh.autobio.modules.adatper.InvoiceDetailAdapter;
import com.yh.autobio.modules.adatper.InvoiceProductAdapter;
import com.yh.autobio.modules.bean.InvoiceBean;
import com.yh.autobio.modules.bean.ProductBean;
import com.yh.autobio.modules.bean.User;
import com.yh.autobio.modules.ui.empty.EmptyLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.yh.autobio.AppConfig.SCN_CUST_ACTION_SCODE;

/**
 * Created by john on 2018/3/5 0005. 16:14
 */

public class InvoiceProductFragment extends BaseFragment {
    @InjectView(R.id.recycleview)
    RecyclerView recycleview;
    @InjectView(R.id.swiperefreshlayout)
    SwipeRefreshLayout swiperefreshlayout;
    @InjectView(R.id.error_layout)
    EmptyLayout errorLayout;
    private InvoiceBean head;

    private BroadcastReceiver mSamDataReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(SCN_CUST_ACTION_SCODE)) {
                String message = intent.getStringExtra(AppConfig.SCN_CUST_EX_SCODE);
                handleOtherText(message);//扫描处理逻辑
            }
        }
    };
    private InvoiceProductAdapter adapter;
    private ArrayList<ProductBean> products;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.fragment_base_recycleview, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        setHasOptionsMenu(true);
        IntentFilter intentFilter = new IntentFilter(SCN_CUST_ACTION_SCODE);
        mActivity.registerReceiver(mSamDataReceiver, intentFilter);
        head = (InvoiceBean) getArguments().getSerializable("HEAD");
        swiperefreshlayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);

        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO: 2016/8/3 下拉刷新
                initData();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        //设置布局管理器
        recycleview.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
//        layoutManager.setOrientation(layoutManager.VERTICAL);
        //设置增加或删除条目的动画
        recycleview.setItemAnimator(new DefaultItemAnimator());
        adapter = new InvoiceProductAdapter(mActivity);
        recycleview.setAdapter(adapter);
    }

    @Override
    public void initData() {
        super.initData();
        products = (ArrayList<ProductBean>) getArguments().getSerializable("PRODUCTS");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            mActivity.unregisterReceiver(mSamDataReceiver);
        } catch (Exception ex) {
        }
        ButterKnife.reset(this);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.box_menu, menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.btn_box:

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void handleOtherText(String text) {
        boolean isHave = false;
        boolean isRight = false;
//        ProductBean bean1=null;
        List<ProductBean> data = adapter.getData();
        for (int i = 0; i < products.size(); i++) {
            ProductBean bean1 = products.get(i);
            if (bean1.getFNumber_s().equals(text)){
                isRight = true;
                for (int j = 0; j < data.size(); j++) {
                    ProductBean bean2 = data.get(j);
                    if (bean2.getFNumber_s().equals(bean1.getFNumber_s())){
                        isHave = true;
//                        bean2.setFDoneNumber(bean2.getFDoneNumber()+1);
                        adapter.notifyItemChanged(j);
                        return;
                    }
                }
                if (!isHave){
//                    bean1.setFDoneNumber(1);
                    adapter.addProduct(bean1);
                }else{
                    Snackbar.make(mActivity.getWindow().getDecorView(),"请扫描清单物料！",Snackbar.LENGTH_SHORT).show();
                }

            }
        }
        if (!isRight){
            Snackbar.make(mActivity.getWindow().getDecorView(),"请扫描正确物料！",Snackbar.LENGTH_SHORT).show();
        }
    }
}
